package com.risk.kernel.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.risk.common.utils.DateUtils;
import com.risk.common.utils.StringUtils;
import com.risk.kernel.domain.DsTripartiteAgreement;
import com.risk.kernel.mapper.DsTripartiteAgreementMapper;
import com.risk.kernel.service.IDsTripartiteAgreementService;
import com.risk.kernel.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Liwen
 * @createTime: 2024/12/1
 * @description:
 */
@Service
public class DsTripartiteAgreementServiceImpl implements IDsTripartiteAgreementService {
    @Autowired
    private DsTripartiteAgreementMapper dsTripartiteAgreementMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${flow.control}")
    private String flowControlKey;

    /**
     * 分页列表查询
     */
    @Override
    public List<DsTripartiteAgreement> selectDsTripartiteAgreementList(String searchValue) {
        return dsTripartiteAgreementMapper.selectDsTripartiteAgreementList(searchValue);
    }


    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public DsTripartiteAgreement selectDsTripartiteAgreementById(String id) {
        return dsTripartiteAgreementMapper.selectDsTripartiteAgreementById(id);
    }


    /**
     * 新增
     *
     * @param dsTripartiteAgreement
     * @return
     */
    @Override
    public int insertDsTripartiteAgreement(DsTripartiteAgreement dsTripartiteAgreement) {
        if (dsTripartiteAgreement.getModelName().equals("wg")) {
            boolean mum = selectAgreementMaximumEntryByServiceCode(dsTripartiteAgreement.getAppCode());
            if (mum) {
                throw new RuntimeException("已到达3条数据上限，无法添加");
            }
            String key = "wg_" + dsTripartiteAgreement.getAppCode();
            if (StringUtils.isEmpty(redisTemplate.opsForValue().get(key))) {
                String value = "3";
                redisTemplate.opsForValue().set(key, value);
            }
        }
        dsTripartiteAgreement.setCreateTime(DateUtils.getNowDate());
        dsTripartiteAgreement.setTrId(RandomUtil.getRandomNumber());
        return dsTripartiteAgreementMapper.insertDsTripartiteAgreement(dsTripartiteAgreement);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param dsTripartiteAgreement 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDsTripartiteAgreement(DsTripartiteAgreement dsTripartiteAgreement) {
        dsTripartiteAgreement.setUpdateTime(DateUtils.getNowDate());
        return dsTripartiteAgreementMapper.updateDsTripartiteAgreement(dsTripartiteAgreement);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDsTripartiteAgreementByIds(String[] ids) {
        return dsTripartiteAgreementMapper.deleteDsTripartiteAgreementByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDsTripartiteAgreementByIds(Long id) {
        return dsTripartiteAgreementMapper.deleteDsTripartiteAgreementById(id);
    }


    /**
     * 根据serviceCode查询网关模块下的数据不能超过3条
     *
     * @param serviceCode
     * @return
     */
    public boolean selectAgreementMaximumEntryByServiceCode(String serviceCode) {
        List<DsTripartiteAgreement> list = dsTripartiteAgreementMapper.selectDsTripartiteAgreementList(serviceCode);
        return list.size() >= 3;
    }

    @Override
    public List<JSONObject> getServiceCodeList(String serviceCode) {
        List<String> list = dsTripartiteAgreementMapper.getServiceCodeList(serviceCode);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        list.forEach(item -> {
            String key = "wg_" + item;
            String status = redisTemplate.opsForValue().get(key);
            if (StringUtils.isEmpty(status)) {
                redisTemplate.opsForValue().set(key, "3");
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("serviceCode", item);
            jsonObject.put("status", status);
            jsonObjectList.add(jsonObject);
        });
        return jsonObjectList;
    }

    @Override
    public int saveServiceCodeCatch(JSONObject jsonObject) {
        try {
            String serviceCode = (String) jsonObject.get("serviceCode");
            String status = (String) jsonObject.get("status");
            String key = "wg_" + serviceCode;
            redisTemplate.opsForValue().set(key, status);
            return 1;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean saveThreadUnlock(boolean lock) {
        try {
            redisTemplate.opsForValue().set(flowControlKey, String.valueOf(lock));
            return lock;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean getThreadUnlock() {
        String lock = redisTemplate.opsForValue().get(flowControlKey);
        if (StringUtils.isEmpty(lock)) {
            saveThreadUnlock(false);
            return false;
        }
        return Boolean.parseBoolean(lock);
    }
}
